Skip to content

fix: preserve null semantics in expression simplification - #24258

Open
fzlzjerry wants to merge 5 commits into
apache:mainfrom
fzlzjerry:fix/24246-null-aware-simplification
Open

fix: preserve null semantics in expression simplification#24258
fzlzjerry wants to merge 5 commits into
apache:mainfrom
fzlzjerry:fix/24246-null-aware-simplification

Conversation

@fzlzjerry

@fzlzjerry fzlzjerry commented Aug 11, 2026

Copy link
Copy Markdown

Which issue does this PR close?

Rationale for this change

Set algebra over IN / NOT IN expressions can erase SQL three-valued-logic semantics when either the tested expression or a list item is nullable. The array_has to IN rewrite has a similar mismatch: an array containing NULL does not behave like an IN list containing NULL.

Preserving those NULL branches is required in general expression contexts. In a filter, however, both FALSE and NULL reject the row, so the optimizer can still produce the strongest equivalent plan there.

What changes are included in this PR?

  • Keep IN / NOT IN set operations unchanged when list items may be NULL, and preserve the NULL branch when an otherwise safe set operation becomes a constant for a nullable tested expression.
  • Avoid rewriting array_has to IN when a nullable array element would change the result, while retaining the safe non-volatile same-expression case.
  • Extend EliminateFilter to simplify positive AND / OR trees according to filter semantics. This restores plans such as EmptyRelation for disjoint IN predicates while retaining correct NULL behavior in projections. The simplification deliberately does not cross NOT or arbitrary expressions.
  • Add unit, runtime, and SQL logic regression coverage for NULL results and the resulting optimized plans.

Are these changes tested?

Yes. The following checks pass:

  • cargo clippy --all-targets --all-features -- -D warnings
  • ./dev/rust_lint.sh
  • cargo test --profile=ci --test sqllogictests
  • prlimit --nofile=65536:65536 -- env RUST_BACKTRACE=1 cargo test --profile ci --exclude datafusion-examples --exclude datafusion-benchmarks --exclude datafusion-cli --workspace --lib --tests --bins --features avro,json,backtrace,extended_tests,recursive_protection,parquet_encryption

Are there any user-facing changes?

Yes. Expressions involving nullable inputs retain their SQL three-valued-logic results, while equivalent filter predicates continue to optimize away rejected rows. There are no public API changes.

Copilot AI lite review requested due to automatic review settings August 11, 2026 09:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Aug 11, 2026
@AdamGS

AdamGS commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

I'll try and review it later today

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.19512% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.01%. Comparing base (a05388e) to head (ea308e2).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...imizer/src/simplify_expressions/expr_simplifier.rs 92.22% 3 Missing and 4 partials ⚠️
datafusion/functions-nested/src/array_has.rs 94.52% 3 Missing and 1 partial ⚠️
datafusion/functions/src/math/log.rs 85.00% 1 Missing and 2 partials ⚠️
datafusion/functions/src/math/power.rs 90.90% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main   #24258    +/-   ##
========================================
  Coverage   81.01%   81.01%            
========================================
  Files        1106     1106            
  Lines      383974   384228   +254     
  Branches   383974   384228   +254     
========================================
+ Hits       311085   311297   +212     
- Misses      54554    54577    +23     
- Partials    18335    18354    +19     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@neilconway

Copy link
Copy Markdown
Contributor

FYI there is also #24247 and #24248

@AdamGS

AdamGS commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

@neilconway thanks for the heads up, I'll try and do a pass on all three tonight/tomorrow morning (UK time).

@fzlzjerry

Copy link
Copy Markdown
Author

Thanks for flagging these. I compared the scopes:

I'll keep the current comparison available for the review pass, then promptly rebase and drop whichever overlapping implementation the maintainers decide not to use.

Comment thread datafusion/sqllogictest/test_files/predicates.slt Outdated
Comment thread datafusion/sqllogictest/test_files/predicates.slt
@AdamGS

AdamGS commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

I think the logical plans that this creates seem worse in some of these trivial cases, which ideally will keep optimizing work away while maintaining correctness in more complex cases.

I'm not sure if its because of the default nullability of stuff coming out the parser or something else.

@fzlzjerry

Copy link
Copy Markdown
Author

Addressed both plan concerns in c2bdac8 and synced the branch with the latest main.

The general expression simplification still preserves NULL in projection contexts, while EliminateFilter now reduces the generated positive AND / OR trees using filter truth semantics. As a result, the disjoint IN case is back to EmptyRelation, and the NOT IN / OR case becomes IS NOT NULL rather than retaining the compound predicate. The latter must not become a bare scan because NULL inputs do not satisfy the filter.

The overlapping log, power, and XOR changes are no longer part of this PR. The remaining 8-file scope passes all-target/all-feature clippy, ./dev/rust_lint.sh, the SQLLogicTest suite, and the extended workspace test matrix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expression simplifications may produce incorrect results with NULLs

6 participants